﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SNMP;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        Session session;
        Entity entity;
        Community community;
        VariableBindingsList vbl;
        PduData pdu;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //vbl = this.session.CreateVariableBindingsList();
            //vbl.Add(".1.3.6.1.2.1.1.1.0", new Value(Value.Syntax.Octets, 0));
            //vbl.Add(".1.3.6.1.2.1.1.1.1", new Value(Value.Syntax.Octets, 0));
            //vbl.Add(".1.3.6.1.2.1.1.1.2", new Value(Value.Syntax.Octets, 0));
            //pdu = this.session.CreatePdu(PduData.PduType.Get, 0, 0, 0, vbl);
            //this.session.SendMessage(entity, entity, community, pdu);

            
            Dictionary<String, Value> dic = this.session.TraverseMIB("192.168.0.89", "public", "1.3.6.1.2.1.1.1.0");
            System.IO.FileStream fs = new System.IO.FileStream(@"F:\mib.txt", System.IO.FileMode.Append);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
            int a = 0;
            foreach (KeyValuePair<String, Value> kvp in dic)
            {
                sw.WriteLine(kvp.Key + "|" + kvp.Value.DataSyntax.ToString() + "|" + kvp.Value.Data.ToString());
                //String tempKey = kvp.Key;
                //Value tempVal = kvp.Value;
                a++;
            }
            int b = a;
            


            /*
            community.Clear();
            entity.Clear();
            vbl.Clear();
            pdu.Clear();
            */
            //VariableBindingsList2 asd = new VariableBindingsList2();
            //asd.Add(new Value2());
            //session.SendMsg();
            //session.Close();
            //status = SNMPManager.Cleanup();
            //Entity entity2 = this.session.CreateEntity("192.168.0.89", 1000, 3);
        }

        uint callback(IntPtr hSession, IntPtr hWnd, uint wMsg, uint wParam, int lParam, IntPtr lpClientData)
        {
            int xus = 1;
            this.session.ReceiveMessage();
            return 1;
        }

        int GetNumber(int n, int m)
        {
            // Console::WriteLine("[managed] callback!");
            return n + m;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Microsoft.Win32.RegistryKey currentUserKey = Microsoft.Win32.Registry.CurrentUser;

            SnmpApiStatus status = SNMPManager.Startup();
            //this.session = SNMPManager.CreateSession(this.Handle, this.callback);
            this.session = SNMPManager.CreateSession(this.Handle);
            this.session.ReceiveData += this.session_ReceiveData;
            this.session.Timeout += this.session_Timeout;
            this.session.ReceiveTrap += this.session_ReceiveTrap;
            
            entity = this.session.CreateEntity("192.168.0.89", 1000, 3);
            community = this.session.CreateCommunity("public");
            vbl = this.session.CreateVariableBindingsList();
            vbl.Add("1.3.6.1.2.1.2.2.1.6.11", new Value(Value.Syntax.Null, System.String.Empty.ToString()));
            //vbl.Add(".1.3.6.1.2.1.1.1.1", new Value(Value.Syntax.Null, 0));
            //vbl.Add(".1.3.6.1.2.1.1.1.2", new Value(Value.Syntax.Null, 0));
            pdu = this.session.CreatePdu(PduData.PduType.Get, 0, 0, 0, vbl);
            
            this.session.Register();
        }

        private void session_ReceiveData(object sender, ReceiveDataEventArgs e)
        {
            int x = 10;
        }

        private void session_ReceiveTrap(object sender, ReceiveDataEventArgs e)
        {
            int x = 10;
        }

        private void session_Timeout(object sender, EventArgs e)
        {
            int x = 10;
        }
    }
}
